//
//  MemberViewController.m
//  ShopXinyi
//
//  Created by qianfeng on 15/9/14.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "MemberViewController.h"
#import "HttpRequestManager.h"
#import "UserInfoModel.h"
#import "LoginViewController.h"
#import "HttpRequestManager.h"
#import "UIImageView+AFNetworking.h"
#import "MemberHeadCell.h"
#import "MemberLittleCell.h"
#import "UserModel.h"


@interface MemberViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSArray *_pictureArray;
    NSArray *_nameArray;
    UserModel *_model;
}
@property (weak, nonatomic) IBOutlet UITableView *myTableView;

@end

@implementation MemberViewController
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        _pictureArray = @[@"row1",@"row2",@"row3",@"row4",@"row5",@"row6"];
        _nameArray = @[@"我要赚钱",@"余额充值",@"账号安全",@"浏览历史",@"积分管理",@"电话客服/订购/建议"];
        _model = [UserModel new];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self requestInfoFromNet];
    
    [_myTableView registerNib:[UINib nibWithNibName:@"MemberHeadCell" bundle:nil] forCellReuseIdentifier:@"HeadCell"];
    [_myTableView registerNib:[UINib nibWithNibName:@"MemberLittleCell" bundle:nil] forCellReuseIdentifier:@"LittleCell"];
    
}

-(void)requestInfoFromNet
{
    UserInfoModel *model = [UserInfoModel shareInstance];
    [[HttpRequestManager shareInstance] getUserInfoWithKey:model.key success:^(id responseObj) {
        NSDictionary *infoDic = responseObj;
        //打印用户信息
        _model.headImage = infoDic[@"avator"];
        _model.name = infoDic[@"user_name"];
        _model.point = infoDic[@"point"];
        _model.predepoint = infoDic[@"predepoit"];
        _model.favourite = infoDic[@"favorites"];
        [_myTableView reloadData];
    } failure:^(NSError *error) {
        NSLog(@"获取用户信息失败,error==%@",error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -UITableViewDelegate&UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else
        return 6;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        MemberHeadCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HeadCell"];
        [cell.headImgView setImageWithURL:[NSURL URLWithString:_model.headImage]];
        cell.nameLabel.text = _model.name;
        cell.pointLabel.text = _model.point;
        cell.predepoitLabel.text = [NSString stringWithFormat:@"¥ %@",_model.predepoint];
        cell.favouriteLabel.text = [NSString stringWithFormat:@"%@",_model.favourite];
        
        return cell;
    }else{
        MemberLittleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LittleCell"];
        cell.imgView.image = [UIImage imageNamed:_pictureArray[indexPath.row]];
        cell.label.text = _nameArray[indexPath.row];
        return cell;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return  289;
    }else
        return _myTableView.bounds.size.width/8;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}

@end
